package com.spring.mvc.controller.ex;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @auther 尚智江
 * @Date 2022/11/3 9:01
 */
public class ExceptionHandler implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("code",500);
        model.put("message",e.getMessage());
        model.put("url",httpServletRequest.getRequestURL());
        //根据不同错误转向不同的页面
        if (e instanceof Ex){
            return new ModelAndView("ex",model);
        }else if (e instanceof exxx){
          return new ModelAndView("500",model);
        } else {
            return new ModelAndView("404",model);

        }

    }
}
